package l2hc.gameserver.tables;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import l2hc.commons.util.Rnd;
import l2hc.gameserver.Config;
import l2hc.gameserver.ThreadPoolManager;
import l2hc.gameserver.model.GameObjectsStorage;

public class FakePlayersTable
{
  private static final Logger _log = Logger.getLogger(FakePlayersTable.class.getName());
  private static String[] _fakePlayers;
  @SuppressWarnings({ "unchecked", "rawtypes" })
  private static List<String> _activeFakePlayers = new ArrayList();
  private static FakePlayersTable _instance;

  public static FakePlayersTable getInstance()
  {
    if (_instance == null)
      new FakePlayersTable();
    return _instance;
  }

  public FakePlayersTable()
  {
    _instance = this;
    if (Config.ALLOW_FAKE_PLAYERS)
    {
      parseData();
      ThreadPoolManager.getInstance().scheduleAtFixedRate(new Task(), 180000L, 1000L);
    }
  }

  @SuppressWarnings({ "rawtypes", "unchecked" })
private static void parseData()
  {
    LineNumberReader lnr = null;
    try
    {
      File doorData = new File("config/fake_players.list");
      lnr = new LineNumberReader(new BufferedReader(new FileReader(doorData)));

      List players_list = new ArrayList();
      String line;
      while ((line = lnr.readLine()) != null)
      {
        if ((line.trim().length() == 0) || (line.startsWith("#")))
          continue;
        players_list.add(line);
      }
      _fakePlayers = (String[])players_list.toArray(new String[players_list.size()]);
      _log.config("FakePlayersTable: Loaded " + _fakePlayers.length + " Fake Players.");
    }
    catch (Exception e1)
    {
      e1.printStackTrace();
    }
    finally
    {
      try
      {
        if (lnr != null)
          lnr.close();
      }
      catch (Exception e1)
      {
      }
    }
  }

  public static int getFakePlayersCount() {
    return _activeFakePlayers.size();
  }

  public static List<String> getActiveFakePlayers()
  {
    return _activeFakePlayers;
  }

  public static class Task
    implements Runnable
  {
    public void run()
    {
      try
      {
        if ((FakePlayersTable._activeFakePlayers.size() < Math.max(0, GameObjectsStorage.getAllPlayersCount() - GameObjectsStorage.getAllOfflineCount()) * Config.FAKE_PLAYERS_PERCENT / 100) && (FakePlayersTable._activeFakePlayers.size() < FakePlayersTable._fakePlayers.length))
        {
          if (Rnd.chance(10))
          {
            String player = FakePlayersTable._fakePlayers[Rnd.get(FakePlayersTable._fakePlayers.length)].toLowerCase();

            if ((player != null) && (!FakePlayersTable._activeFakePlayers.contains(player)))
              FakePlayersTable._activeFakePlayers.add(player);
          }
        }
        else if (FakePlayersTable._activeFakePlayers.size() > 0)
         FakePlayersTable._activeFakePlayers.remove(Rnd.get(FakePlayersTable._activeFakePlayers.size()));
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
  }
}
